package com.gilbertoca.igreja.view.util;

import com.gilbertoca.igreja.view.Application;
import com.gilbertoca.igreja.model.util.TipoGenericoSimple;
import com.gilbertoca.igreja.service.base.BaseService;
import com.gilbertoca.igreja.view.dialogs.ConfirmationDialog;
import com.gilbertoca.igreja.view.dialogs.WarningDialog;
import com.gilbertoca.igreja.view.util.models.TipoGenericoSimpleTableModel;
import java.util.List;
import javax.swing.JFrame;
import org.jdesktop.application.Action;

public class TiposGenericosSimplesDialog<T extends TipoGenericoSimple> extends javax.swing.JDialog
         implements IGenericAbms{

    Boolean nuevo;
    Class<? extends TipoGenericoSimple> clazz;

    TipoGenericoSimple seleccionado;
    
    public TiposGenericosSimplesDialog(java.awt.Frame parent, boolean modal,Class<? extends TipoGenericoSimple> clazz,String titulo) {
        super(parent, modal);
        initComponents();
        setTitle(titulo);
        this.clazz = clazz;
        refresh();
    }

    public void refresh(){
        List tipos = null;
        tipos = BaseService.get().getAll(clazz);
        tabla.setModel(new TipoGenericoSimpleTableModel(tipos));
    }

    
    @Action
    public void alta() {
        JFrame mainFrame = Application.getApplication().getMainFrame();
        TipoGenericosimpleDialogDialog caracterUsuarioDialog = null;
        TipoGenericoSimple tipo = null;
        try {
            tipo = (TipoGenericoSimple)clazz.newInstance();
        } catch (Exception c) {
        }
        caracterUsuarioDialog = new TipoGenericosimpleDialogDialog(mainFrame,true,tipo);
        caracterUsuarioDialog.setLocationRelativeTo(mainFrame);
        Application.getApplication().show(caracterUsuarioDialog);
        refresh();
    }
    
    @Action
    public void modificacion() {
        select();
        
        JFrame mainFrame = Application.getApplication().getMainFrame();
        
        TipoGenericosimpleDialogDialog caracterUsuarioDialog = new TipoGenericosimpleDialogDialog(mainFrame,true,seleccionado);
        caracterUsuarioDialog.setLocationRelativeTo(mainFrame);
        Application.getApplication().show(caracterUsuarioDialog);

        refresh();
    }

    @Action
    public void baja() {
         if(ConfirmationDialog.show("Desea eliminar el elemento seleccionado?")) {
                select();
                seleccionado.setBorrado(Boolean.TRUE);
                try{
                    BaseService.get().update(seleccionado);
                }catch(Exception ex) {
                }
                refresh();
        }
    }

    @Action
    public void cerrar(){
        this.dispose();
    }

    public void select() {
        int row = tabla.getSelectedRow();
        if (row >= 0) {
            seleccionado = (TipoGenericoSimple) ((TipoGenericoSimpleTableModel)tabla.getModel()).getElements().get(row);
        } else {
            WarningDialog.show("Debe Seleccionar un Elemento de la Lista");
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tabla = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        altaBtn = new javax.swing.JButton();
        modBtn = new javax.swing.JButton();
        bajaBtn = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N
        setResizable(false);

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tabla.setName("tabla"); // NOI18N
        jScrollPane1.setViewportView(tabla);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setName("jPanel1"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(com.gilbertoca.igreja.view.Application.class).getContext().getActionMap(TiposGenericosSimplesDialog.class, this);
        altaBtn.setAction(actionMap.get("alta")); // NOI18N
        altaBtn.setName("altaBtn"); // NOI18N
        altaBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                altaBtnActionPerformed(evt);
            }
        });

        modBtn.setAction(actionMap.get("modificacion")); // NOI18N
        modBtn.setName("modBtn"); // NOI18N

        bajaBtn.setAction(actionMap.get("baja")); // NOI18N
        bajaBtn.setName("bajaBtn"); // NOI18N

        jButton1.setAction(actionMap.get("cerrar")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, altaBtn, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
                    .add(modBtn, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
                    .add(bajaBtn, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
                    .add(jButton1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(altaBtn)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(modBtn)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(bajaBtn)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 167, Short.MAX_VALUE)
                .add(jButton1)
                .addContainerGap())
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 249, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 307, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void altaBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_altaBtnActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_altaBtnActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton altaBtn;
    private javax.swing.JButton bajaBtn;
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton modBtn;
    private javax.swing.JTable tabla;
    // End of variables declaration//GEN-END:variables

}
